package com.xml.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.xml.domain.Reader;
import com.xml.utils.HibernateUtils;

public class ReaderDaoImpl implements ReaderDao {

	@Override
	public Reader insertReader(Reader reader) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		
		session.save(reader);
		
		transaction.commit();
		session.close();
		return reader;
	}

	@Override
	public boolean deleteReader(int id) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			Reader reader = (Reader) session.load(Reader.class, id);
			session.delete(reader);
			session.getTransaction().commit();
		}catch (Exception e) {
			if(transaction!=null){
				transaction.rollback();
				return false;}
		}finally{
			session.close();
		}
		return true;
	}

	@Override
	public boolean updateReader(Reader reader) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			Reader reader1 = (Reader) session.load(Reader.class, reader.getId());
			reader1.setPassword(reader.getPassword());
			reader1.setReaderName(reader.getReaderName());
			reader1.setTelephone(reader.getTelephone());
			reader1.setSex(reader.getSex());
			session.update(reader1);
			transaction.commit();
		}catch (Exception e) {
			if(transaction!=null){
				transaction.rollback();
				return false;}
		}finally{
			session.close();
		}
		return true;
	}

	@Override
	public List<Reader> searchReader() {
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		List<Reader> result = session.createQuery("from Reader").list();
		transaction.commit();
		session.close();
		return result;
	}

	@Override
	public List<Reader> pagingQuery(int pageNo, int pageSize) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		
		Query query = session.createQuery("from Reader");
		query.setFirstResult(pageNo);
		query.setMaxResults(pageSize);
		List<Reader> list = query.list();
		
		transaction.commit();
		session.close();
		
		return list;
	}

	@Override
	public Reader query_byId(int id) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		Reader reader = (Reader) session.get(Reader.class, id);
		transaction.commit();
		session.close();
		return reader;
	}

	@Override
	public boolean checkReader(String id) {
		try{
			Session session = HibernateUtils.getSession();
			Transaction transaction = session.beginTransaction();
			Query query = session.createQuery("from Reader where readerId = ?");
			query.setParameter(0,id);
			Reader reader = (Reader) query.list().get(0);
			transaction.commit();
			session.close();
		}catch (Exception e) {
			return false;
		}
		return true;
	}

	@Override
	public int numOfBorrow(String id) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createQuery("from Reader where readerId = ?");
		query.setParameter(0,id);
		Reader reader = (Reader) query.list().get(0);
		transaction.commit();
		session.close();
		return reader.getNumOfBorrow();
	}

	@Override
	public void updateNumOfBorrow(int id) {
		
		// TODO Auto-generated method stub

	}

	@Override
	public void setReaderId() {
		Session session = HibernateUtils.getSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			Query query = session.createQuery("from Reader where readerId = ?");
			query.setParameter(0,"9999");
			Reader reader = (Reader) query.list().get(0);
			int id = reader.getId();
			String readerID = HibernateUtils.getSysYear()+id;
			while(readerID.length()<9){
				readerID = readerID+"0";
			}
			reader.setReaderId(readerID);
			session.update(reader);
			transaction.commit();
		}catch(Exception e){
			if(transaction!=null)
				transaction.rollback();
		}finally{
			session.close();
		}
				
				
		
		
	}
	
	

}
